package org.hnxxxy.ebms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.mapper.RoleMapper;
import org.hnxxxy.ebms.pojo.*;
import org.hnxxxy.ebms.mapper.ExpenseMapper;
import org.hnxxxy.ebms.service.ExpenseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hnxxxy.ebms.service.RecruitStuService;
import org.hnxxxy.ebms.service.TeacherAdmissService;
import org.hnxxxy.ebms.util.CommonUntil;
import org.hnxxxy.ebms.vo.ExpenseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-10
 */
@Service
public class ExpenseServiceImpl extends ServiceImpl<ExpenseMapper, Expense> implements ExpenseService {

    @Autowired
    private RecruitStuService recruitStuService;

    @Autowired
    private TeacherAdmissService teacherAdmissService;

    @Autowired
    private ExpenseMapper expenseMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result<String> add(Expense expense) {
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        RecruitStu recruitStu = recruitStuService.getById(role.getUserId());
        // 通过地区id查询到老师
        QueryWrapper<TeacherAdmiss> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admission_id",recruitStu.getAdmissionId());
        TeacherAdmiss one = teacherAdmissService.getOne(queryWrapper);;
        expense.setTeacherId(one.getTeacherId());
        expense.setRecruitStuId(role.getUserId());
        if (this.save(expense)){
            return Result.success(200,"添加成功");
        }
        return Result.fail("添加失败");
    }

    @Override
    public Result<IPage<ExpenseVo>> getExpense(int page) {
        IPage<ExpenseVo> iPage = new Page<>(page,10);
        Integer id = CommonUntil.getId();
        Role role = roleMapper.selectById(id);
        IPage<ExpenseVo> expenseIPage = null;
        if (role.getIsTeacher()){
            // 学生
            expenseIPage = expenseMapper.selectExpensePage(iPage,role.getUserId());
        }else {
            // 老师
            expenseIPage = expenseMapper.selectTeaExpensePage(iPage,role.getUserId());
        }
        return Result.success(expenseIPage);
    }
}
